/**
 * @file{同步cube_device 表}
 */
'use strict';

const {
    source_db_config,
    aim_db_config
} = require('../config.json').cube_db;

const sequelize_source = require('../lib/db').get_instance(source_db_config).sequelize;
const sequelize_aim = require('../lib/db').get_instance(aim_db_config).sequelize;

/**
 * 同步cube表的类
 */
class SynchronizeCubeDevice {

    cache_cubeData = []; // 缓存的cubedata

    constructor () {
        this.getSourceCubeInfo();
    }

    static getInstance () {
        return SynchronizeCubeDevice.instance ? SynchronizeCubeDevice.instance : new SynchronizeCubeDevice();
    }
    /**
     * 获取产线cube信息
     */
    async getSourceCubeInfo () {
        let sql =  "select * from cube_device";
        let res = await sequelize_source.query(sql, { type: sequelize_source.QueryTypes.SELECT });
        this.postAimCubeInfo(res);
    }
    /**
     * 上传到总库的cube
     * @param {Array} data 产线cube的数据
     */
    async postAimCubeInfo (data) {
        let updateData = this.judgeIsUpdate(data);
        // 不需要更新
        if (!updateData) {
            return false;
        }
        //

    }
    /**
     * 判断是否需要更新
     * @param {array} data 新查询的cube数据
     * @returns 返回需要更新的字段
     */
    judgeIsUpdate (data) {
        let updateData = [];
        data.forEach((item, index) => {
            if (item.device_name !== this.cache_cubeData[index].device_name || item.device_id !== this.cache_cubeData[index].device_id) {
                updateData.push(item);
            }
        })
        return updateData;
    }
}

module.exports = SynchronizeCubeDevice;